<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE 测试页面</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 0;
            margin: 0 auto;
            height: 100vh;
        }
        .panel{
            width: 1280px;
            height: 100vh;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
        }

        .left-panel, .right-panel {
            flex: 1;
            padding: 20px;
            box-sizing: border-box;
            height: 100%;
            overflow-y: auto;
        }

        .left-panel {
            border-right: 1px solid #ccc;
            display: flex;
            flex-direction: column;
        }

        .right-panel {
            width: 600px;
            display: flex;
            flex-direction: column;
        }

        .message-container {
            flex: 1;
            border: 1px solid #ddd;
            margin-bottom: 10px;
            padding: 10px;
            overflow-y: auto;
            background-color: #f9f9f9;
        }

        .message {
            margin-bottom: 8px;
            padding: 8px;
            background-color: #e3f2fd;
            border-radius: 4px;
        }

        .input-area {
            display: flex;
            gap: 10px;
        }

        #messageInput {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        #sendButton {
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        #sendButton:hover {
            background-color: #45a049;
        }

        .chart-container {
            height: 300px;
        }

        .metrics-info {
            margin-top: 0;
        }

        h2 {
            margin-top: 0;
            color: #333;
        }
    </style>
</head>
<body>
<div class="panel">
    <!-- 左侧面板 - SSE 消息展示 -->
    <div class="left-panel">
        <h2>SSE 消息</h2>
        <div id="messageContainer" class="message-container">
            <!-- 消息将在这里动态添加 -->
        </div>
        <div class="input-area">
            <input type="text" id="messageInput" placeholder="输入消息内容...">
            <button id="sendButton">发送</button>
        </div>
    </div>

    <!-- 右侧面板 - 请求指标图表 -->
    <div class="right-panel">
        <h2>请求指标</h2>
        <div class="chart-container">
            <canvas id="statusCodeChart"></canvas>
        </div>
        <div class="chart-container">
            <canvas id="pathChart"></canvas>
        </div>
        <div class="metrics-info">
            <h3>指标数据</h3>
            <pre id="metricsData">正在加载指标数据...</pre>
        </div>
    </div>
</div>


<script>
    // SSE 连接
    const eventSource = new EventSource('http://0.0.0.0:8888/sse');
    const messageContainer = document.getElementById('messageContainer');

    eventSource.onmessage = function(e) {
        const message = document.createElement('div');
        message.className = 'message';
        message.textContent = e.data;
        messageContainer.appendChild(message);
        messageContainer.scrollTop = messageContainer.scrollHeight;
    };

    eventSource.onerror = function() {
        console.error('SSE 连接出错');
    };

    // 发送消息
    const sendButton = document.getElementById('sendButton');
    const messageInput = document.getElementById('messageInput');

    sendButton.addEventListener('click', sendMessage);
    messageInput.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });

    function sendMessage() {
        const msg = messageInput.value.trim();
        if (msg) {
            fetch('http://0.0.0.0:8888/sendToGroup', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    clientId: 'test-client',
                    msg: msg
                })
            }).then(response => {
                if (!response.ok) {
                    console.error('发送失败');
                }
            }).catch(error => {
                console.error('发送错误:', error);
            });

            messageInput.value = '';
        }
    }

    // 图表初始化
    const statusCodeCtx = document.getElementById('statusCodeChart').getContext('2d');
    const pathCtx = document.getElementById('pathChart').getContext('2d');

    const statusCodeChart = new Chart(statusCodeCtx, {
        type: 'pie',
        data: {
            labels: [],
            datasets: [{
                data: [],
                backgroundColor: [
                    'rgba(54, 162, 235, 0.7)',
                    'rgba(255, 99, 132, 0.7)',
                    'rgba(255, 206, 86, 0.7)',
                    'rgba(75, 192, 192, 0.7)',
                    'rgba(153, 102, 255, 0.7)'
                ]
            }]
        },
        options: {
            responsive: true,
            plugins: {
                title: {
                    display: true,
                    text: '请求状态码分布'
                }
            }
        }
    });

    const pathChart = new Chart(pathCtx, {
        type: 'pie',
        data: {
            labels: [],
            datasets: [{
                data: [],
                backgroundColor: [
                    'rgba(255, 99, 132, 0.7)',
                    'rgba(54, 162, 235, 0.7)',
                    'rgba(255, 206, 86, 0.7)',
                    'rgba(75, 192, 192, 0.7)',
                    'rgba(153, 102, 255, 0.7)'
                ]
            }]
        },
        options: {
            responsive: true,
            plugins: {
                title: {
                    display: true,
                    text: '请求路径分布'
                }
            }
        }
    });

    // 获取指标数据
    function fetchMetrics() {
        fetch('http://0.0.0.0:8080/metrics')
            .then(response => response.json())
            .then(data => {
                document.getElementById('metricsData').textContent = JSON.stringify(data, null, 2);

                // 更新状态码图表
                if (data.statusCodes) {
                    statusCodeChart.data.labels = Object.keys(data.statusCodes);
                    statusCodeChart.data.datasets[0].data = Object.values(data.statusCodes);
                    statusCodeChart.update();
                }

                // 更新路径图表
                if (data.paths) {
                    pathChart.data.labels = Object.keys(data.paths);
                    pathChart.data.datasets[0].data = Object.values(data.paths);
                    pathChart.update();
                }
            })
            .catch(error => {
                console.error('获取指标失败:', error);
                document.getElementById('metricsData').textContent = '获取指标失败: ' + error.message;
            });
    }

    // 初始加载指标数据
    fetchMetrics();

    // 每5秒刷新一次指标数据
    setInterval(fetchMetrics, 5000);
</script>
</body>
</html>